<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.keyword" placeholder="微信昵称 | 认证名称 | 联系人 | 电话" style="width: 300px;" class="filter-item" @input="handleSearch" />
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" size="mini" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="认证名称">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.address.consignee }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" width="120px">
        <template slot-scope="{row}">
          <span>{{ row.address.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址">
        <template slot-scope="{row}">
          <span>{{ row.address.district + row.address.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门面照片" width="100px">
        <template slot-scope="{row}">
          <el-image
            v-if="row.store_images.length > 0"
            style="width: 80px; height: 80px;"
            :src="row.store_images[0].url"
          />
        </template>
      </el-table-column>
      <el-table-column label="用户" width="180px">
        <template slot-scope="{row}">
          <el-image
            style="width: 80px; height: 80px;"
            :src="row.user.profile.avatar_url"
          />
          <span>{{ row.user.profile.nick_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100px">
        <template slot-scope="{row}">
          <el-tag
            v-if="row.state === 'ACCEPTED'"
            effect="dark"
            type="success"
          >
            已通过
          </el-tag>
          <el-tag
            v-if="row.state === 'REJECTED'"
            effect="dark"
            type="danger"
          >
            已拒绝
          </el-tag>
          <el-tag
            v-if="row.state === 'PENDING'"
            effect="dark"
          >
            审核中
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center">
        <template slot-scope="{row}">
          <el-popconfirm
            v-if="row.state === 'PENDING'"
            title="是否确认审核通过？"
            @onConfirm="handleUpdate(row, 'accept')"
          >
            <el-button slot="reference" size="mini" type="success">
              审核通过
            </el-button>
          </el-popconfirm>
          <el-popconfirm
            v-if="row.state === 'PENDING'"
            title="是否确认拒绝申请？"
            @onConfirm="handleUpdate(row, 'reject')"
          >
            <el-button slot="reference" size="mini" type="danger">
              拒绝通过
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, updateEnrollment } from '@/api/enrollment'
import Pagination from '@/components/Pagination'

export default {
  name: 'Enrollment',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        nickname:null,
        keyword: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增'
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.enrollments
        this.total = response.total
        this.listLoading = false
      }).catch(error => {
        this.list = []
        this.total = 0
        this.listLoading = false
        Promise.reject(error)
      })
    },
    handleUpdate(row, action) {
      const that = this
      updateEnrollment(row.id, action).then(response => {
        this.$notify({
          title: '成功',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
        const index = that.list.findIndex(v => v.id === response.enrollment.id)
        this.list.splice(index, 1, response.enrollment)
      }).catch(error => {
        Promise.reject(error)
      })
    },
    handleSearch: function() {
      this.list = []
      this.listQuery.page = 1
      this.getList()
    }
  }
}
</script>
